/**
 * yiqihao(一起好商务股份有限公司) copyright 
 */
package com.p2p.backend.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.p2p.base.util.biz.ModelViewUtil;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.facade.UserTenderFacade;
import com.p2p.front.result.UserTenderResult;
import com.p2p.front.service.LoanVariationService;
import com.p2p.front.service.TenderService;

/**
 *
 * @author leiruiqi@yiqihao.com
 *
// * 2015年4月1日 上午9:05:54
 */
@RequestMapping("/tender")
@Controller
public class TenderController extends BaseController{

	private final Logger logger = LoggerFactory
			.getLogger(TenderController.class);
	
	@Resource
	private TenderService tenderService;
	
	@Resource
	private LoanVariationService loanVariationService;
	
	@Resource
	private UserTenderFacade userTenderFacade;
	
	
	@RequestMapping("/queryTenderByUid")
	public void queryTenderByUid(Long uid,int page,int rows,HttpServletResponse response){
		
		PageInfo pageInfo = tenderService.queryPageByUserId(uid, page, rows);
		
		Map<String,Object> data = new HashMap<String,Object>();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("res", 1);
		jsonObject.put("data", data);
		
		data.put("total", pageInfo.getTotal());
		List<String> nameList = new ArrayList<String>();
		
		/* {field:'tid',checkbox:true},
         {field:'uid',title:'用户id'},
         {field:'lid',title:'贷款id'},
			{field:'userName',title:'用户名'} ,
			{field:'loanApr',title:'年利率'} ,
			{field:'loanRepayMethod',title:'还款方式',formatter:formatRepayMethod},
			{field:'amount',title:'投标金额'},
			{field:'num',title:'期数'},
			{field:'startNum',title:'开始期数'},
			{field:'status',title:'投标状态',formatter:formatStatus},
			{field:'tstatus',title:'转让状态',formatter:formatTStatus}*/
		nameList.add("tid");
		nameList.add("uid");
		nameList.add("lid");
		nameList.add("userName");
		nameList.add("loanApr");
		nameList.add("loanRepayMethod");
		nameList.add("amount");
		nameList.add("num");
		nameList.add("startNum");
		nameList.add("status");
		nameList.add("tstatus");
		nameList.add("loanTime");
		nameList.add("addtime");
		List mapModelViewList = ModelViewUtil.getMapModelViewList(pageInfo.getList(), nameList);
		data.put("rows",
				mapModelViewList);
		this.returnJsonObject(response, jsonObject);
		return;
	}
	
	@RequestMapping("/doCancelTender")
	public void doCancelTender(Long tid,Long uid,Long lid,HttpServletResponse response){
		UserTenderResult result = userTenderFacade.doTenderCancelBiz(tid, uid, lid);
		logger.error("result = "+result);
		this.returnMessage(response, String.valueOf(result.isSuccess()));
	}
	
	@RequestMapping("/cancelTender")
	public String cancelTender(){
		return "tender/cancelTender";
	}
}
